//
// Created by DELL on 2023/10/13.
//

#include <Windows.h>
#include <cstdio>

int main() {
    HKEY hKey;
    DWORD dwType = 0;
    TCHAR subKeyName[MAX_PATH]{};
    TCHAR value[MAX_PATH * 2]{};
    DWORD dwSize = MAX_PATH;

    const TCHAR reg[] = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion");

    // 打开注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
    LONG result = RegOpenKeyEx(
        HKEY_LOCAL_MACHINE,
        reg,
        0,
        KEY_READ,
        &hKey
    );
    wprintf(L"Reg Key: HKEY_LOCAL_MACHINE\\%s\n\n", reg);

    if (result == ERROR_SUCCESS) {
        printf("Sub keys:\n");

        // 枚举子项名称
        DWORD index = 0;
        while (true) {
            dwSize = sizeof(subKeyName) / sizeof(TCHAR);
            result = RegEnumKeyEx(
                hKey,
                index,
                subKeyName,
                &dwSize,
                nullptr,
                nullptr,
                nullptr,
                nullptr
            );

            if (result == ERROR_SUCCESS) {
                //printf("%s\n", subKeyName);
                wprintf(L"%s\n", subKeyName);
                index++;
            }
            else if (result == ERROR_NO_MORE_ITEMS) {
                // 所有子项都已枚举完毕
                break;
            }
            else {
                printf("Failed to enumerate subKey.\n");
                break;
            }
        }

        dwSize = sizeof(value) - sizeof(TCHAR);
        result = RegGetValue(hKey, L"", L"", RRF_RT_ANY, &dwType, value, &dwSize);
        if (result == ERROR_SUCCESS || result == ERROR_MORE_DATA)
        {
            wprintf(L"Default Value: %s\n", value);
        }
        else
        {
            wprintf(L"Failed to read Default value! \n");
        }


        printf("Values:\n");
        index = 0;
        while (true)
        {
            dwSize = sizeof(subKeyName) / sizeof(TCHAR);
            result = RegEnumValue(
                hKey,
                index,
                subKeyName,
                &dwSize,
                nullptr,
                nullptr,
                nullptr,
                nullptr
            );
            index++;

            if (result == ERROR_SUCCESS) {
                dwSize = sizeof(value) - sizeof(TCHAR);
                result = RegGetValue(hKey, L"", subKeyName, RRF_RT_ANY, &dwType, value, &dwSize);

                if (result == ERROR_SUCCESS || result == ERROR_MORE_DATA)
                {
                    wprintf(L"%s=%s\n", subKeyName, value);
                }
                else
                {
                    wprintf(L"Failed to read value: %s\n", subKeyName);
                }
            }
            else if (result == ERROR_NO_MORE_ITEMS) {
                // 所有子项都已枚举完毕
                break;
            }
            else {
                printf("Failed to enumerate value.\n");
                break;
            }
        }

        // 关闭注册表项句柄
        RegCloseKey(hKey);
    }
    else {
        printf("Failed to open registry key.\n");
    }

    wprintf(L"Reg Key: HKEY_LOCAL_MACHINE\\%s\n", reg);

    return 0;
}